#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import sys, datetime, time, os, logging
import ibm_db
import re
import json

from apscheduler.schedulers.blocking import BlockingScheduler

from config import app_config, ProductionConfig, DevelopmentConfig
from app.utils.XUtils import XUtils
from app.utils.XLogger import XLogger
from app.TestStateJob import TestStateJob
from app.FDCLStateJob import FDCLStateJob
from app.RCMStateJob import RCMStateJob
from app.SummaryJob import SummaryJob
from app.ColdRollingJob import ColdRollingJob
from app.utils.XDB2Utils import XDB2Utils


def main(p_argv):
    profile = p_argv[1] if len(p_argv) > 1 else 'dev'
    #
    config = app_config[profile]
    if os.getenv('DB_HOST') is not None:
        config.DB_HOST = os.environ.get('DB_HOST')
        config.DB_PORT = os.environ.get('DB_PORT')
        config.DB_USER = os.environ['DB_USER']
        config.DB_PASSWORD = os.environ['DB_PASSWORD']
        config.DB_INSTANCE_NAME = os.environ['DB_INSTANCE_NAME']
        config.DB_CHARSET = os.environ['DB_CHARSET']



    scheduler = BlockingScheduler()
    # NOTE 并行 10分钟一次
    # scheduler.add_job(func=run_job, args=[config], trigger='cron', minute='*/10', id="run_job", max_instances=1,
    #                   next_run_time=datetime.datetime.now())
    # NOTE 每天01:00分执行该定时任务
    scheduler.add_job(func=run_job, args=[config], trigger='cron', day='*', hour='1', minute='0')
    scheduler.start()

    return 0


def run_job(p_config=None):
    SummaryJob(p_config=p_config).execute()
    pass


if __name__ == '__main__':
    info = XUtils.get_git_info()
    XLogger.instance().logger.info('\n%s' % json.dumps(info, sort_keys=True, indent=4))

    start = datetime.datetime.now()

    status = main(sys.argv)

    elapsed = float((datetime.datetime.now() - start).seconds)
    print("Time Used 4 All ----->>>> %f seconds" % (elapsed))

    sys.exit(status)
